(function(){
	'use strict';
	angular.module('controllers.workOrderData.publishMgmt')
	.controller('PublishStatisticController',PublishStatisticController);
	/**
	 * 衡量指标
	 * h5001：发布版本统计
	 * h5002：发布阶段天数统计
	 */
	function PublishStatisticController($scope,workOrderDataService,CoreLoading,PublicFn,$timeout,publishStatisticFactory,COREDATA){
		//初始化查询衡量指标
		$scope.initQuery = function(){
			workOrderDataService.queryMeasureIndex({code:'5'},function(res){
				$scope.measureIndexs = res.data;
				$scope.search();
			});
		};

		$scope.flag = {
			state: true, //查询选项的拉上拉下
			show: 'view', //show值为view时显示图，值为table时显示表格,在directive/switchingButton.js里面更改
			arrow: false, //控制查询选项箭头显示或隐藏
		};

		//导出路径
		$scope.formPrefix = COREDATA.FORMPREFIX+'workOrderData/publishMgmt/export';
		//页面绑定查询选项初始化，时间格式为日
		$scope.sendData = workOrderDataService.getSendData('1');
		//默认衡量指标为发布版本统计
		$scope.sendData.measureIndexCode = 'h5001';
		//获取DOM元素并生成Echarts实例
		var myEcharts = echarts.init(document.getElementById('doc'));
		//查询方法
		$scope.search = function(){
			$scope.flag.state = !$scope.flag.state;
			var code,option;
			var xAxisData = [];//Echarts x轴数据
			var seriesData = [];//Echarts 图形数据
			if(!$scope.flag.state){
				//格式化需要发送给后端的数据，例如将时间变成字符串
				$scope.send = workOrderDataService.fmtSendData($scope.sendData);
				//时间标题
				var timeTitle = PublicFn.fmtDateTitle($scope.send);
				//衡量指标code
				code = $scope.send.measureIndexCode;
				//查询等待样式
				CoreLoading.setState(true);
				//查询后端数据方法
				publishStatisticFactory.query($scope.send,function(res){
					//根据后端返回的数据显示不同的处理结果
					if(res.state === '10000' && res.data.length >=1){
						myEcharts.clear();
						$scope.items = res.data;
						//根据衡量指标获取对应的名字
						$scope.measureIndexName = workOrderDataService.getName(code,$scope.measureIndexs,'measureIndexCode','measureIndexName');
						//不同的衡量指标选项选择不同的显示结果
						$scope.send.title = timeTitle + '各业务系统' + $scope.measureIndexName;
						if(code === 'h5001'){
							//页面表格和图形标题
							option = publishStatisticFactory.getEchartsData(res.data,$scope.send.title);
							option.tooltip.formatter = formatter();
							$scope.change = true;
						}else {
							angular.forEach(res.data,function(rd){
								xAxisData.push(rd.sysName);
								seriesData.push(rd.avgDay);
							});
							option = workOrderDataService.getEchartsData(xAxisData,seriesData,$scope.send.title,'发布阶段平均天数');
							option.grid = {
								x2:$scope.measureIndexName.length * 12 + 50
							};
							option.yAxis[0] = workOrderDataService.formatterYAxis('单位:(天)','');
							option.tooltip.formatter = workOrderDataService.getFormatterFn('天');
							$scope.change = false;
						}
						myEcharts.setOption(option);
						$scope.outData = workOrderDataService.exportView(myEcharts,$scope.send);
						$scope.flag.arrow = true;
						$scope.flag.show = 'view';
					}else if(res.data.length === 0){
						workOrderDataService.queryNoDataOrError($scope.flag,'查询无数据');
					}else {
						workOrderDataService.queryNoDataOrError($scope.flag,'查询错误');
					}
					CoreLoading.setState(false);
				});
			}
		};

		$scope.$watch('sendData.measureIndexCode',function(newValue){
			newValue === '1' ? $scope.sendData.changeTypeCode = '1' : $scope.sendData.changeTypeCode = '';
		});
		//监控页面时间，如果时间出错，则查询按钮禁止
		$scope.$watch('myForm.$invalid', function(newValue) {
			newValue ? $scope.flag.arrow = false : $scope.flag.arrow = true;
		});
		function formatter(){
			return function(params){
				if(params.seriesName === '一次性通过率'){
					return params.seriesName + ' </br>' + params.name + ':' + params.value + '%';
				}else if(params.seriesName === '打回次数') {
					return params.seriesName + ' </br>' + params.name + ':' + params.value + '次';
				}else {
					return params.seriesName + ' </br>' + params.name + ':' + params.value + '个';
				}
			};
		}
	}
	PublishStatisticController.$inject = ['$scope','workOrderDataService','CoreLoading','PublicFn','$timeout','publishStatisticFactory','COREDATA'];
})();